package cn.ekuma.pos.pda.sale;

import com.openbravo.bean.sales.TicketLine;
import cn.ekuma.android.ui.InputView;
import cn.ekuma.epos.bean.util.sale.TicketLineHelper;
import cn.ekuma.pos.android.pos.R;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class TicketLineEditor extends LinearLayout{
	
	private Button btn_jname,btn_junits,btn_jprice,btn_jpricerant,btn_jtotal,btn_ticketline_ok;
	TicketLine editLine;

	OnSelectedActionListener l;
	
	public interface OnSelectedActionListener {   
	        void onOkAction(TicketLine editLine);
	}
	    
	public void setOnSelectedActionListener(OnSelectedActionListener l){
	  	this.l=l;
	}
	
	public TicketLineEditor(Context context) {
		super(context);
		init(context);
	}
	
	 private void init(final Context context) {
	    	// ��ȡҳ�沼�� 
	        View mainView = LayoutInflater.from(context).inflate( 
	                        R.layout.pos_ticketline_edit, this); 
	        btn_jname=(Button) mainView.findViewById(R.id.btn_jname);
	        
	        btn_junits=(Button) mainView.findViewById(R.id.btn_junits);
	        btn_junits.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
			    	AlertDialog.Builder builder = new AlertDialog.Builder(context);
			    	InputView inputView=new InputView(context);
			        builder.setView(inputView);
			        final AlertDialog dialog=builder.create();
			        inputView.setOnSelectedActionListener(new InputView.OnSelectedActionListener(){
						public void onOkAction(double value) {
							editLine.setMultiply(value);
							dialog.dismiss();
							resert(editLine);
						}
			        }); 
			        inputView.reset(editLine.getMultiply());
			        dialog.show();  
				}
	        	
	        });
	        
	        btn_jprice=(Button) mainView.findViewById(R.id.btn_jprice);
	        btn_jprice.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
			    	InputView inputView=new InputView(context);
			        builder.setView(inputView);
			        final AlertDialog dialog=builder.create();
			        inputView.setOnSelectedActionListener(new InputView.OnSelectedActionListener(){
						public void onOkAction(double value) {
							editLine.setPrice(value);
							dialog.dismiss();
							resert(editLine);
						}
			        }); 
			        inputView.reset(editLine.getPrice());
			        dialog.show();  
				}
	        	
	        });
	        
	        
	        btn_jpricerant=(Button) mainView.findViewById(R.id.btn_jpricerant);
	        btn_jpricerant.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
			    	InputView inputView=new InputView(context);
			        builder.setView(inputView);
			        final AlertDialog dialog=builder.create();
			        inputView.setOnSelectedActionListener(new InputView.OnSelectedActionListener(){
						public void onOkAction(double value) {
							editLine.setPriceRate(value);
							dialog.dismiss();
							resert(editLine);
						}
			        }); 
			        inputView.reset(editLine.getPriceRate());
			        dialog.show();  
				}
	        	
	        });
	        
	        btn_jtotal=(Button) mainView.findViewById(R.id.btn_jtotal);
	        
	        
	        btn_ticketline_ok=(Button) mainView.findViewById(R.id.btn_ticketline_ok);
	        btn_ticketline_ok.setOnClickListener(new OnClickListener(){
				public void onClick(View v) {
					if(l!=null)
						l.onOkAction(editLine);	
				}
	        });
	 }
	
	 
	 public void resert(TicketLine l){
		 this.editLine=l;
		 btn_jname.setText(TicketLineHelper.printName(l));
		 btn_junits.setText(TicketLineHelper.printMultiply(l));
		 btn_jprice.setText(TicketLineHelper.printPrice(l));
		 btn_jpricerant.setText(TicketLineHelper.printPriceRate(l));
		 btn_jtotal.setText(TicketLineHelper.printValue(l));
	 }

}
